/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software Foundation,
 *    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package JET.Actions;

import JET.Centers.ControlCenter;
import JET.Scene;
import JET.physics.steering.Torque;
import java.util.ArrayList;
import org.w3c.dom.*;

/**
 *
 * @author 057Ry
 */
public class ActivateTorquesAction extends Action
{
    static final ControlCenter CC = ControlCenter.getInstance();
    
    private static int id = 0;
    private ArrayList<Torque> torquesList;

    public ActivateTorquesAction(String name, ArrayList<Torque> tList)
    {
        //super(id>0 ? name+id : name);
        super(name);
        torquesList = new ArrayList<Torque>(tList);
        id++;
    }

    public ActivateTorquesAction(Element actionXMLElement, ArrayList<Torque> tList)
    {
        super(id>0 ? "ActivateTorquesAction"+id : "ActivateTorquesAction");
        torquesList = new ArrayList<Torque>();

        NodeList children  = actionXMLElement.getChildNodes();

        for(int i=0; i<children.getLength(); i++)
        {
            Node node = children.item(i);
            if (node instanceof Element)
            {
                Element elem = (Element)node;

                // Mass
                if (elem.getTagName().equals("name") )
                {
                    Text textNode = (Text) elem.getFirstChild();
                    this.setName( textNode.getData().trim() );
                }
                else if (elem.getTagName().equals("torques") )
                {
                    NodeList torquesNodes = elem.getChildNodes();

                    for(int ti=0; ti<torquesNodes.getLength(); ti++)
                    {
                        Node tNode = torquesNodes.item(ti);
                        if (tNode instanceof Element)
                        {
                            Element tElem = (Element)tNode;

                            if(tElem.getTagName().equals("torque_index"))
                            {
                                Text textNode = (Text) tElem.getFirstChild();
                                String text = textNode.getData().trim();
                                int tIndex = Integer.parseInt(text);
                                torquesList.add( tList.get(tIndex) );
                            }
                        }
                    }
                }
            }
        }
    }
    public void addTorque(Torque t)
    {
        torquesList.add(t);
    }
    public void addToriques(ArrayList<Torque> tList)
    {
        torquesList.addAll(tList);
    }
    public void clearTorques()
    {
        torquesList.clear();
    }
    public void removeTorque(Torque t)
    {
        torquesList.remove(t);
    }

    @Override
    public void call()
    {
        for(Torque t : torquesList)
            t.setActive(true);
    }

    @Override
    public String toString()
    {
        return this.getName();
    }

    public static int getActualID()
    {
        return id;
    }

    /**
     *
     * @param doc
     * @param cc
     * @return
     */
    @Override
    public Element getXMLRepresentation(Document doc, Scene scene)
    {
        Element rootElement = doc.createElement("action");
        rootElement.setAttribute("class_name", this.getClass().getName());

        rootElement.appendChild(doc.createTextNode("\n"));

        Element nameEl = doc.createElement("name");
        nameEl.appendChild(doc.createTextNode(getName()));
        rootElement.appendChild(nameEl);

        rootElement.appendChild(doc.createTextNode("\n"));

        Element tListEl = doc.createElement("torques");
        for(Torque t : torquesList)
        {
            Element torqueIndexEl = doc.createElement("torque_index");
            int tIndex = scene.getJETContext().getTorqueList().indexOf(t);
            torqueIndexEl.appendChild( doc.createTextNode(""+tIndex));
            tListEl.appendChild(doc.createTextNode("\n"));
            tListEl.appendChild(torqueIndexEl);
        }
        rootElement.appendChild(tListEl);

        rootElement.appendChild(doc.createTextNode("\n"));
        return rootElement;

    }
}
